package factory;

import java.util.HashMap;

import loadsave.Memento;
import loadsave.loadsaveobject.Iterator;
import loadsave.loadsaveobject.LoadSaveMapComposite;
import loadsave.loadsaveobject.LoadSaveObject;
import loadsave.loadsaveobject.LoadSaveObjectComposite;
import loadsave.loadsaveobject.LoadSavePrimitive;
import occupation.Occupation;

public class OccupationFactory implements loadsave.LoadSaveable {

    private static java.util.Map<String, Occupation> occMap;
    
    /**
     * create the static map
     */
    static {
        occMap = new java.util.HashMap<String, Occupation>();
    }
    
    /**
     * return an Occupation based on the passing in string
     * @param occName
     * @return
     */
    public static Occupation createOccupation( String occName ) {
        return occMap.get(occName).clone();
    }
    
    /**
     * add the given Occupation to the map
     * @param o
     */
    public static void addOccupation(Occupation o) {
    	occMap.put(o.getName(), o);
    }
    
    public Memento getMemento() {
    	return new Memento() {

			@Override
			public LoadSaveObject getState() {
				LoadSaveObjectComposite toReturn = new LoadSaveObjectComposite("occupationFactory", "OccupationFactory");
				LoadSaveMapComposite lsmc = new LoadSaveMapComposite("occMap", "HashMap", "String", "Occupation");
				for (String s : occMap.keySet()) {
					lsmc.add(new LoadSavePrimitive("","",s), occMap.get(s).getMemento().getState());
				}
				toReturn.add(lsmc.getName(), lsmc);
				return toReturn;
			}

			@Override
			public void restoreState(LoadSaveObject lso) {
				OccupationFactory.occMap = new HashMap<String, Occupation>();
				Iterator<LoadSaveObject> i = lso.get("occMap").getIterator();
				for (i.init(); i.hasNext(); i.next()) {
					Occupation toAdd = new Occupation();
					toAdd.getMemento().restoreState(i.getCurrVal());
					OccupationFactory.occMap.put(i.getCurrKey().getValue(), toAdd);
				}
			}
    		
    	};
    }
}
